package com.example.demo.view;

import com.example.demo.dao.StaffDao;
import com.example.demo.model.Staff;
import com.example.demo.util.StringUtil;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Font;

@SuppressWarnings("serial")
public class MainFrm extends JFrame {

    private JTable staffListTable;
    private ButtonGroup sexButtonGroup;
    private JButton deleteStaffButton;
    private JButton searchButton;
    private JLabel ynameJlabel;
    private JTextField ynameTextField;
    private JTextField searchTextField;
    private DefaultTableModel model;
    private JScrollPane jscrollpane;
    private JLabel sexJLabel;
    private JRadioButton MRadioButton;
    private JRadioButton FRadioButton;
    private JLabel birthJlabel;
    private JTextField birthTextField;
    private JLabel homeJlabel;
    private JTextField homeTextField;
    private JLabel degreeJlabel;
    private JComboBox degreeJComboBox;
    private JLabel salaryJlabel;
    private JTextField salaryTextField;
    private JLabel infoJlabel;
    private JTextArea infoTextArea;
    private JButton addButton;
    private JButton resetButton;
    private JButton editButton;
    public static Object userObject;

    public MainFrm(Object userObject) {
        this.userObject = userObject;
        setTitle("员工管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
        setBounds(200, 20, 1200, 700);//设置页面大小
        setResizable(false);//设置页面不可拖拽改变大小
        getContentPane().setLayout(null);
        setVisible(true);

        searchTextField = new JTextField();
        searchTextField.setBounds(250, 10, 180, 30);
        getContentPane().add(searchTextField);
        searchTextField.setColumns(10);

        searchButton = new JButton("搜索");
        searchButton.setBounds(450, 10, 95, 30);
        getContentPane().add(searchButton);
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                searchStaff(ae);
            }
        });

        Object[] header = {"员工号","员工姓名","性别","出生日期","籍贯","学历","工资","备注"};
        model = new DefaultTableModel(header,0);
        staffListTable = new JTable(model);
        jscrollpane = new JScrollPane(staffListTable);
        jscrollpane.setBounds(10, 50, 800, 600);
        getContentPane().add(jscrollpane);
        setTable(new Staff());//展示员工信息列表
        staffListTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                selectedTableRow(me);
            }
        });

        ynameJlabel =new JLabel("员工姓名：");
        ynameTextField=new JTextField();
        ynameJlabel.setBounds(860, 50, 100, 30);
        ynameTextField.setBounds(950, 50, 200, 30);
        getContentPane().add(ynameJlabel);
        getContentPane().add(ynameTextField);

        sexJLabel = new JLabel("性      别：");
        sexJLabel.setBounds(860, 100, 74, 23);
        getContentPane().add(sexJLabel);
        MRadioButton = new JRadioButton("男");
        MRadioButton.setBounds(975, 100, 74, 23);
        getContentPane().add(MRadioButton);
        FRadioButton = new JRadioButton("女");
        FRadioButton.setBounds(1051, 100, 74, 23);
        getContentPane().add(FRadioButton);
        sexButtonGroup = new ButtonGroup();
        sexButtonGroup.add(MRadioButton);
        sexButtonGroup.add(FRadioButton);
        MRadioButton.setSelected(true);

        birthJlabel =new JLabel("出生日期：");
        birthTextField=new JTextField();
        birthJlabel.setBounds(860, 150, 100, 30);
        birthTextField.setBounds(950, 150, 200, 30);
        getContentPane().add(birthJlabel);
        getContentPane().add(birthTextField);

        homeJlabel =new JLabel("籍      贯：");
        homeTextField=new JTextField();
        homeJlabel.setBounds(860, 200, 100, 30);
        homeTextField.setBounds(950, 200, 200, 30);
        getContentPane().add(homeJlabel);
        getContentPane().add(homeTextField);

        degreeJlabel =new JLabel("学      历：");
        degreeJComboBox =new JComboBox();
        degreeJlabel.setBounds(860, 250, 100, 30);
        degreeJComboBox.setBounds(950, 250, 200, 30);
        getContentPane().add(degreeJlabel);
        getContentPane().add(degreeJComboBox);

        salaryJlabel =new JLabel("工      资：");
        salaryTextField=new JTextField();
        salaryJlabel.setBounds(860, 300, 100, 30);
        salaryTextField.setBounds(950, 300, 200, 30);
        getContentPane().add(salaryJlabel);
        getContentPane().add(salaryTextField);

        infoJlabel =new JLabel("备      注：");
        infoTextArea=new JTextArea();
        infoJlabel.setBounds(860, 350, 100, 30);
        infoTextArea.setBounds(950, 350, 200, 80);
        getContentPane().add(infoJlabel);
        getContentPane().add(infoTextArea);

        JLabel remindLabel = new JLabel("出生日期应为YYYY-MM-DD或YYYY.MM.DD类型,否则添加失败");
        remindLabel.setFont(new Font("宋体", Font.PLAIN, 13));
        remindLabel.setForeground(Color.RED);
        remindLabel.setBounds(820, 440, 360, 32);
        getContentPane().add(remindLabel);

        addButton =new JButton("新增员工");
        addButton.setBounds(820, 500, 90, 30);
        getContentPane().add(addButton);
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                staffAddAct(ae);
            }
        });
        setEduInfo();

        editButton =new JButton("修改信息");
        editButton.setBounds(910, 500, 90, 30);
        getContentPane().add(editButton);
        editButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                submitEditAct(ae);
            }
        });

        resetButton =new JButton("重置");
        resetButton.setBounds(1000, 500, 90, 30);
        getContentPane().add(resetButton);
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                resetValue(ae);
            }
        });

        deleteStaffButton =new JButton("删除员工");
        deleteStaffButton.setBounds(1090, 500, 90, 30);
        getContentPane().add(deleteStaffButton);
        deleteStaffButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                deleteStaff(ae);
            }
        });

    }
    //查询员工信息
    protected void searchStaff(ActionEvent ae) {
        // TODO 自动生成的方法存根
        Staff staff = new Staff();
        staff.setYname(searchTextField.getText().toString());
        setTable(staff);
    }
    //删除员工信息
    protected void deleteStaff(ActionEvent ae) {
        // TODO 自动生成的方法存根
        int row = staffListTable.getSelectedRow();
        if(row == -1){
            JOptionPane.showMessageDialog(this, "请选中要删除的数据！");
            return;
        }
        if(JOptionPane.showConfirmDialog(this, "您确定删除么？") != JOptionPane.OK_OPTION){
            return;
        }
        StaffDao staffDao = new StaffDao();
        if(staffDao.delete(Integer.parseInt(staffListTable.getValueAt(row, 0).toString()))){
            JOptionPane.showMessageDialog(this, "删除成功！");
        }else{
            JOptionPane.showMessageDialog(this, "删除失败！");
        }
        staffDao.closeDao();
        setTable(new Staff());
    }
    //列表展示员工信息
    public void setTable(Staff staff) {
        model =(DefaultTableModel) staffListTable.getModel();
        model.setRowCount(0);
        StaffDao staffDao=new StaffDao();
        List<Staff> StaffList = staffDao.getStaffList(staff);
        for(Staff s:StaffList) {
            Vector v=new Vector();
            v.add(s.getYno());
            v.add(s.getYname());
            v.add(s.getSex());
            v.add(s.getBirth());
            v.add(s.getHome());
            v.add(s.getDegree());
            v.add(s.getSalary());
            v.add(s.getInfo());
            model.addRow(v);
        }
        staffDao.closeDao();
    }
    //修改员工信息
    protected void submitEditAct(ActionEvent ae) {
        // TODO 自动生成的方法存根
        int row = staffListTable.getSelectedRow();
        if(row == -1){
            JOptionPane.showMessageDialog(this, "请选中要修改的数据！");
            return;
        }
        String staffName = ynameTextField.getText().toString();
        String birth = birthTextField.getText().toString();
        String home = homeTextField.getText().toString();
        String degree =(String) degreeJComboBox.getSelectedItem();
        String sex = MRadioButton.isSelected() ? MRadioButton.getText() : FRadioButton.getText();
        Double salary = Double.parseDouble(salaryTextField.getText().toString());
        String info = infoTextArea.getText().toString();
        if(StringUtil.isEmpty(staffName)){
            JOptionPane.showMessageDialog(this, "请填写员工姓名！");
            return;
        }
        if(StringUtil.isEmpty(home)){
            JOptionPane.showMessageDialog(this, "请填写员工籍贯！");
            return;
        }
        Staff staff = new Staff();
        staff.setYname(staffName);
        staff.setYno(Integer.parseInt(staffListTable.getValueAt(row, 0).toString()));
        if(MRadioButton.isSelected())staff.setSex(MRadioButton.getText().toString());
        if(FRadioButton.isSelected())staff.setSex(FRadioButton.getText().toString());
        staff.setSex(sex);
        staff.setBirth(birth);
        staff.setHome(home);
        staff.setDegree(degree);
        staff.setSalary(salary);
        staff.setInfo(info);
        StaffDao staffDao = new StaffDao();
        if(staffDao.update(staff)){
            JOptionPane.showMessageDialog(this, "更新成功！");
        }else{
            JOptionPane.showMessageDialog(this, "更新失败！");
        }
        staffDao.closeDao();
        setTable(new Staff());
    }
    //重置按钮
    private void resetValue(ActionEvent ae) {
        // TODO 自动生成的方法存根
        ynameTextField.setText("");
        birthTextField.setText("");
        infoTextArea.setText("");
        homeTextField.setText("");
        salaryTextField.setText("");
        degreeJComboBox.setSelectedIndex(0);
        sexButtonGroup.clearSelection();
        MRadioButton.setSelected(true);
    }
    //添加员工信息
    protected void staffAddAct(ActionEvent ae) {
        // TODO Auto-generated method stub
        String staffName = ynameTextField.getText().toString();
        String birth = birthTextField.getText().toString();
        String home = homeTextField.getText().toString();
        Double salary = Double.parseDouble(salaryTextField.getText().toString());
        String info = infoTextArea.getText().toString();
        if(StringUtil.isEmpty(staffName)){
            JOptionPane.showMessageDialog(this, "请填写员工姓名!");
            return;
        }
        if(StringUtil.isEmpty(home)){
            JOptionPane.showMessageDialog(this, "请填写员工籍贯！");
            return;
        }
        String degree =(String) degreeJComboBox.getSelectedItem();
        String sex = MRadioButton.isSelected() ? MRadioButton.getText() : FRadioButton.getText();
        Staff staff = new Staff();
        staff.setYname(staffName);
        staff.setSex(sex);
        staff.setBirth(birth);
        staff.setHome(home);
        staff.setDegree(degree);
        staff.setSalary(salary);
        staff.setInfo(info);
        StaffDao staffDao = new StaffDao();
        if(staffDao.addStaff(staff)){
            JOptionPane.showMessageDialog(this, "添加成功!");
            setTable(new Staff());
        }else{
            JOptionPane.showMessageDialog(this, "添加失败!");
        }
        resetValue(ae);
    }
    //学历下拉框内容
    private void setEduInfo(){
        degreeJComboBox.addItem("文盲");
        degreeJComboBox.addItem("小学");
        degreeJComboBox.addItem("初中");
        degreeJComboBox.addItem("高中");
        degreeJComboBox.addItem("专科");
        degreeJComboBox.addItem("本科");
        degreeJComboBox.addItem("研究生");
        degreeJComboBox.addItem("博士");
    }
    //实现列表与员工信息内容联动
    private void selectedTableRow(MouseEvent me) {
        model = (DefaultTableModel) staffListTable.getModel();
        ynameTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 1).toString());
        birthTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 3).toString());
        String sex = model.getValueAt(staffListTable.getSelectedRow(), 2).toString();
        sexButtonGroup.clearSelection();
        if(sex.equals(MRadioButton.getText()))MRadioButton.setSelected(true);
        if(sex.equals(FRadioButton.getText()))FRadioButton.setSelected(true);
        homeTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 4).toString());
        degreeJComboBox.setSelectedItem(model.getValueAt(staffListTable.getSelectedRow(), 5).toString());
        salaryTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 6).toString());
        infoTextArea.setText(model.getValueAt(staffListTable.getSelectedRow(), 7).toString());
    }
}
